package com.zhangzhan.gulimallproduct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangzhan.gulimallcommcon.util.PageUtils;
import com.zhangzhan.gulimallcommcon.util.Query;
import com.zhangzhan.gulimallproduct.dao.BrandDao;
import com.zhangzhan.gulimallproduct.dao.CategoryBrandRelationDao;
import com.zhangzhan.gulimallproduct.dao.CategoryDao;
import com.zhangzhan.gulimallproduct.entity.BrandEntity;
import com.zhangzhan.gulimallproduct.entity.CategoryBrandRelationEntity;
import com.zhangzhan.gulimallproduct.entity.CategoryEntity;
import com.zhangzhan.gulimallproduct.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> catelogList(Map<String, Object> params) {
        String brandId = (String) params.get("brandId");

        return this.list(Wrappers.<CategoryBrandRelationEntity>lambdaQuery()
                .eq(CategoryBrandRelationEntity::getBrandId,brandId));

    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //新增商品和分类中间表 并且存入冗余字段来提高检索效率 避免重复查询非中间表次数

        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();
        BrandEntity brand = brandDao.selectById(brandId);
        CategoryEntity category = categoryDao.selectById(catelogId);
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(category.getName());

        this.save(categoryBrandRelation);


    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandName(name);
        this.update(categoryBrandRelationEntity,Wrappers.<CategoryBrandRelationEntity>lambdaUpdate()
                .eq(CategoryBrandRelationEntity::getBrandId,brandId));
    }

    @Override
    public void updateCategory(Long catId, String name) {
        this.baseMapper.updateCategory(catId,name);
    }

    @Override
    public List<CategoryBrandRelationEntity> brandListByCatId(Long catId) {
        return this.list(Wrappers.<CategoryBrandRelationEntity>lambdaQuery().eq(CategoryBrandRelationEntity::getCatelogId,catId));
    }

}